testStopSzenario <- function(groupcode, pattern, direction="long", duration=10, Pversion=0, verbose=TRUE,
                             literations=15, witerations=10, table="orderLogicTest") {
  if('package:PerformanceAnalytics' %in% search() || require('PerformanceAnalytics',quietly=TRUE)) {}
  
  insertSqlString <- function() {
    sql <- sprintf("INSERT INTO `va_stratlab`.`%s` 
      (`id`, `group`, `pattern`, `symbol`, `Pversion`, `method`, 
      `stoplosslimit`, `stopwinlimit`, `direction`, `duration`, 
      `numberoftrades`, `pctstopped`, `stoplosspct`, `stopwinpct`, 
      `returnmean`, `returnsd`, `returnddev`, `pctwin`)", table)
    values <- sprintf("VALUES
      (NULL, '%s', '%s', '%s', '%d', '%s', 
      '%f', '%f', '%s', '%d',
      '%d', '%f', '%f', '%f', 
      '%f', '%f', '%f', '%f')",
      groupcode, pattern, "", Pversion, "fixedPct1", 
      lossLimit*100, winLimit*100, direction, duration,
      ex["numberOfTrades",], ex["stopPct",], ex["stoplossPct",], 
      ex["stopwinPct",], ex["returnMean",], ex["returnSd",], 
      ex["returnDDev",], ex["pctWin",])
    return (paste(sql, values))
  }
  con <- connectToVadb()
  groupinfo <- getDBGroup(groupcode)  # retrieve table with stock_id, code, from, to
  patternlist <- getCSPGroupPattern(groupcode, Pversion=Pversion)  # contains all members of a group
  patternlistnames <- names(patternlist)

  lossLimitInterval <- .01
  winLimitInterval  <- .02
  for (jl in 0:literations) {
    lossLimit <- jl*lossLimitInterval
    for (jw in 0:witerations) {
      winLimit <- jw*winLimitInterval
      returnsdf <- data.frame()
      if (verbose==TRUE) {
        cat ("lossLimit: ", lossLimit, "winLimit: ", winLimit, "\n")
      }
      for (i in 1:length(patternlist)) {  # loop throught all group members
        myOrderXTS <- patternlist[[i]][,pattern]                # i.e. Hammer of DJ
        symname <- paste(patternlistnames[i], ".vadb", sep="")  # i.e. DJ.vadb
        fromtodates <- getGroupMember(groupinfo, patternlistnames[i])
        subsetstring = paste(fromtodates[1], "/", sep="")       # ensure that TS and myOrderXTS have same indexes
        OHLC <- get(symname)[subsetstring]
        if (direction=="long") {
          OrderTS <- merge(myOrderXTS, 0, 
                           (lossLimit>0) * Cl(OHLC)*(1-lossLimit)*myOrderXTS[,1], 
                           (winLimit>0) * Cl(OHLC)*(1+winLimit)*myOrderXTS[,1])
          colnames(OrderTS) <- c("long", "short", "stoploss", "stopwin")
        } else if (direction=="short") {
          OrderTS <- merge(myOrderXTS, 0,
                           (lossLimit>0) * Cl(OHLC)*(1+lossLimit)*myOrderXTS[,1], 
                           (winLimit>0) * Cl(OHLC)*(1-winLimit)*myOrderXTS[,1])
          colnames(OrderTS) <- c("short", "long", "stoploss", "stopwin")
        }
        if (verbose==TRUE) {
          cat ("calculate", patternlistnames[i], "\n")
        }
        OL <- orderLogic(OHLC, OrderTS, n=duration)
        OLDF <- as.data.frame(OL)
      
        if (dim(returnsdf)[1] == 0) {
          if (dim(OLDF)[1] >= 1) {  # rbind only if pattern have been found
            OLDF$symbol <- patternlistnames[i]
            returnsdf <- OLDF
          }      
        } else {
          if (dim(OLDF)[1] >= 1) {  # rbind only if pattern have been found
            OLDF$symbol <- patternlistnames[i]
            returnsdf <- rbind(returnsdf, OLDF)
          }      
        }
      }
      # now the returns can be examined
      ex<- examineOrderTSDF(returnsdf)
      # write observations to db
      dbSendQuery(con, insertSqlString())
    }
  }
  # disconnect from vadb
  disconnectFromVadb(con)
  return(returnsdf)
}